package com.artifact.mit.mypoint.activity;

import static com.artifact.mit.mypoint.util.Variable.PREFS_NAME;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.artifact.mit.mypoint.R;
import com.artifact.mit.mypoint.fragment.LeftMenuFragment;
import com.artifact.mit.mypoint.util.APICall.ImageLoader;
import com.artifact.mit.mypoint.util.SetImage;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MyPointBaseActivity extends SlidingFragmentActivity {

	protected LeftMenuFragment mFrag;
	protected ImageView menuPhoto;
	protected TextView name;
	protected TextView point;
	
	public void onCreate(Bundle savedInstanceState, int i) {
		super.onCreate(savedInstanceState);
		setBehindContentView(R.layout.left_nav_menu);
		Bundle bundle = new Bundle();
		bundle.putInt("menuCheckedIndex", i);
		if (savedInstanceState == null) {
			FragmentTransaction t = this.getSupportFragmentManager()
					.beginTransaction();
			mFrag = new LeftMenuFragment();
			mFrag.setArguments(bundle);
			t.replace(R.id.menu_frame, mFrag);
			t.commit();
		} else {
			mFrag = (LeftMenuFragment) this.getSupportFragmentManager()
					.findFragmentById(R.id.menu_frame);
			mFrag.getArguments().putAll(bundle);
			
		}

		SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
		menuPhoto = (ImageView) findViewById(R.id.menuPhoto);
		new ImageLoader().execute(new SetImage(menuPhoto, preferences.getString("image", ""), true));
		
		name = (TextView) findViewById(R.id.navName);
		name.setText(preferences.getString("name", ""));
		point = (TextView) findViewById(R.id.navPoint);
		point.setText(preferences.getString("point", "0"));
		
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setCustomView(R.layout.actionbar);

		getSupportActionBar().getCustomView().findViewById(R.id.textView1)
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						toggle();
					}
				});
	}
	
	public void setProgressBar(int visibility){
		getSupportActionBar().getCustomView().findViewById(R.id.progressBar1).setVisibility(visibility);
	}
	
	public void setPoint(String point){
		this.point.setText(point);
	}
}
